package com.gitee.feizns.quickstart.web.converter;

import com.gitee.feizns.dynamic.reflect.Constructors;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.lang.NonNull;

/**
 * 枚举值转换器
 * @author feizns
 * @since 2025/3/3
 */
public class EnumValueConverterFactory implements ConverterFactory<String, EnumValue<?>> {

    @Override
    public <T extends EnumValue<?>> Converter<String, T> getConverter(@NonNull Class<T> targetType) {
        T instance = Constructors.newInstance(targetType);
        //noinspection unchecked
        return source -> (T) instance.convert(source);
    }

}
